package com.jie.jie.mvp.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jie.jie.R;
import com.jie.jie.mvp.model.MvpdataBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jie on 2017/1/22.
 */

public class MvpMeiziRvAdapter extends RecyclerView.Adapter<MvpMeiziRvAdapter.ViewHolder> {
    private List<MvpdataBean.ResultsBean> datas;
    private Context context;
    private ItemClick itemClick;
    private List<Integer> heights;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public MvpMeiziRvAdapter(Context context, List<MvpdataBean.ResultsBean> datas) {
        this.context = context;
        this.datas = datas;
        getRandomHeight(datas);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.mvp_item2, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Glide.with(context).load(datas.get(position).getUrl()).diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.mvpIv);
        holder.mvpIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (itemClick != null) {
                    itemClick.click(v,datas.get(position).getUrl(),datas.get(position).getDesc());
                }
            }
        });


    }

    private void getRandomHeight(List<MvpdataBean.ResultsBean> lists){//得到随机item的高度
        heights = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            heights.add((int)(200+Math.random()*400));
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface ItemClick {
        void click(View view, String url,String title);
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.mvp_iv)
        ImageView mvpIv;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
